$(function () {
    obj.init();
});

var obj = new function() {
    return {
        applyForm: null,
        form: null,
        init: function () {
            this.applyForm = $('#reference_schedule_form');
            this.form = new xhForm(this.applyForm);
            this.form.parse();
            this.funObj().addData();//获取select数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_modal_confirm',window.top.document).click(function () {
                $this.funObj().saveData();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                addData: function () {
                    xhExtra.ajaxRequest('res/getReferAllSelectData', {}, {check:true}, function (data) {
                        var classObj = $('#class').select2({placeholder: '选择年级',data: data.class});
                        var subjectObj = $('#subject').select2({placeholder: '选择科目',data: data.subject});
                        var appObj = $('#app').select2({placeholder: '选择应用',data: data.app});
                        var volumeObj = $('#volume').select2();
                        if (modify === 2) {//修改
                            classObj.val([grade]).trigger('change');
                            subjectObj.val([subject]).trigger('change');
                            volumeObj.val([volume]).trigger('change');
                            appObj.val([app]).trigger('change');
                        }
                    });
                },
                saveData: function () {
                    var grade = $('#class').val();
                    var subject = $('#subject').val();
                    var bookName = $('#bookName').val();
                    var buyUrl = $('#buyUrl').val();
                    var typeIn = $('#typeIn').val();
                    var app = $('#app').val();
                    var url = '';
                    if (modify === 1) {
                        if (grade < 1 || subject < 1 || bookName < 1 || buyUrl < 1 || typeIn < 1 || app < 1){
                            window.top.xhTop.showError('请填写完整信息');
                            xhExtra.setBtnNormal($('#bt_modal_confirm', window.top.document));
                            return false;
                        }
                    }
                    if (modify === 1){//创建
                        url = 'res/saveReferenceSchedule';
                    }else{//修改
                        url = 'res/saveModifyReferSchedule';
                    }
                    xhExtra.ajaxRequest(url, {iId: iId, grade: grade, subject: subject,bookName:bookName,
                        remark: $('#remark').val(), buyUrl: buyUrl,typeIn: typeIn, volume: $('#volume').val(),app: app
                    }, {success: true, check:false},function (res) {
                        if (res.code === 200) {
                            window.top.xhTop.showSuccess(res.msg);
                            window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                            window.top.xhTop.closeModal();
                        } else {
                            window.top.xhTop.showError(res.error);
                            xhExtra.setBtnNormal($('#bt_modal_confirm',window.top.document));
                        }
                    });
                }
            }
        }
    }
};
